package edu.mtu.citizenscience.mushroommapper.activity.main.reports;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.cs.utils.Image;
import edu.mtu.citizenscience.mushroommapper.activity.R;
import edu.mtu.citizenscience.mushroommapper.model.ModelReport;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Report List Adapter adapts reports into a time taken, species name and picture on the reports list
 */
public class ReportListAdapter extends ArrayAdapter<ModelReport> {
	// android-query library
	private AQuery aq;
	
	/**
	 * Create the Reports List Adapter
	 * 
	 * @param aq - AQuery - reference to housing activities android query instance
	 * @param mushrooms - List<ModelReport> - list of mushroom reports
	 * @return void
	 */
	public ReportListAdapter(AQuery aq, List<ModelReport> reports) 
	{
		// create array adapter with manange reports list report layout and the list of report models
		super(aq.getContext(), R.layout.manage_reports_list_report, reports);
		
		// attach the android query intance for parent activity
		this.aq = aq;
	}

	/**
	 * Get the view for a Report List report
	 * 
	 * @param int - position - the views position in the list
	 * @param convertView - View - the view for the list item
	 * @param parent - ViewGroup - containing list view
	 * @return View - the populated view for the list item
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// check if the view has not been inflated before
	    if (convertView == null) 
	    {
	    	// Inflate the new view
	    	convertView = this.aq.inflate(convertView, R.layout.manage_reports_list_report, parent);
	    }
	    
	    // get the model report that this view is displaying in the list
	    ModelReport report = this.getItem(position);
	    
	    // get the report time text view and set to a formated version of the report date
	    ((TextView) convertView.findViewById(R.id.reportTime)).setText(new SimpleDateFormat("MM-dd-yy hh:mm a", Locale.US).format(report.getDate()));
	    // get the species name text view and set it to the genus name concatenated with the species name
	    ((TextView) convertView.findViewById(R.id.speciesName)).setText(report.getGenus() + " " + report.getSpecies());
	    
	    // check to see if the report has a photo one
	    if (report.getPhotoOne().getId() > -1)
	    {
	    	// get the report image view and set it to the report photo one scaled to 90x90px
	    	Image.scaleImage(report.getPhotoOne().getPath(), (ImageView) convertView.findViewById(R.id.reportImage), 90, 90);
	    }
	    // check to see if the report has a photo two
	    else if (report.getPhotoTwo().getId() > -1)
	    {
	    	// get the report image view and set it to the report photo two scaled to 90x90px
	    	Image.scaleImage(report.getPhotoTwo().getPath(), (ImageView) convertView.findViewById(R.id.reportImage), 90, 90);
	    }
	    // check to see if the report has a photo three
	    else if (report.getPhotoThree().getId() > -1)
	    {
	    	// get the report image view and set it to the report photo three scaled to 90x90px
	    	Image.scaleImage(report.getPhotoThree().getPath(), (ImageView) convertView.findViewById(R.id.reportImage), 90, 90);
	    }
	    
	    // return the report list item view
	    return convertView;
	  }
}
